package com.itheima.wechat;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    SetmealService setmealService;

    @Autowired
    JedisUtil jedisUtil;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals = new ArrayList<>();
        //从redis拿到数据
        String setmealValue = jedisUtil.get("setmelKey");
        if (setmealValue != null && setmealValue != "") {
            List<Setmeal> setmeals1 = JSON.parseArray(setmealValue, Setmeal.class);
            return  Result.success("",setmeals1);

            /* //对字符串进行分割
             String[] setmealStr = setmealValue.split("},");
            //字符数组中第一个字符串开头为"[",我们不需要，所以得分割
            String[] split1 = setmealStr[0].split("\\[");
            setmealStr[0] = split1[1];
            //字符数组中最后一个字符串结尾为"]",我们不需要，所以得分割
            String[] split2 = setmealStr[setmealStr.length-1].split("\\]");
            setmealStr[setmealStr.length-1] = split2[0];
            for (int i = 0; i < setmealStr.length-1; i++) {
                setmealStr[i] = setmealStr[i] + "}";
            }
            for (String s : setmealStr) {
                Setmeal setmeal = JSON.parseObject(s, Setmeal.class);
                System.out.println(s);
                setmeals.add(setmeal);
            }*/
        } else {
            //从数据库中获取数据
            setmeals = setmealService.getSetmeal();
            //将数据库存放在redis中
            jedisUtil.setex("setmelKey", 60*120, JSON.toJSONString(setmeals));
            return Result.success("",setmeals);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        String setmeal1 = jedisUtil.get("setmeal"+id);
        if (setmeal1 != null && setmeal1 != "") {
            Setmeal setmeal = JSON.parseObject(setmeal1, Setmeal.class);
            System.out.println("setmeal = " + setmeal);
            System.out.println("从redis中获取数据");
            return Result.success("", setmeal);
        } else {
            //从数据库中拿取数据
            Setmeal setmeal = setmealService.findByIdBatch58(id);
            //向redis存入数据
            String setmeal2 = jedisUtil.setex("setmeal"+id, 60 * 120, JSON.toJSONString(setmeal));
            System.out.println("setmeal = " + setmeal2);
            System.out.println("已经把数据存入redis中");
            return Result.success("", setmeal);
        }
    }

    @RequestMapping("/findDetailById")
    public Result findDetailById(Integer id){
        Setmeal setmeal = setmealService.findDetailById(id);
        return Result.success("",setmeal);
    }

    @RequestMapping("/getToken")
    public Result getToken(){
        String token = UUID.randomUUID().toString();
        jedisUtil.setex(token,60 * 30,token);
        return Result.success("",token);
    }

    public static void main(String[] args) {
        String url = "http://localhost:84/pages/setmeal_detail.html?id=12&age=19&name=jack";
        System.out.println(getParam(url,"id"));
        System.out.println(getParam(url,"name"));
        System.out.println(getParam(url,"age"));
    }

    private static String getParam(String url,String name){
        //根据？拆分  ["http://localhost:84/pages/setmeal_detail.html","id=12&age=19&name=jack"]
        String[] urlArray = url.split("\\?");

        String paramStr = urlArray[1];//"id=12&age=19&name=jack"
        //根据& 拆分 ["age=19","id=12","name=jack"]
        String[] paramArray = paramStr.split("&");

        for (String param : paramArray) {
            String key = param.split("=")[0];
            String value = param.split("=")[1];
            if(key.equals(name)){
                return value;
            }
        }
        return "";
    }

}
